package com.tuanzi.loan.web.bean.statistics.creditCardKeeper;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.joda.time.DateTime;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.service.statistics.CreditCardKeeperStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.creditCardKeeper.CreditCardKeeperStatRequest;
import com.tuanzi.loan.vo.response.statistics.creditCardKeeper.CreditCardKeeperStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.CreditCardKeeperStatistisLazyDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;

/**
 * 信用卡管家统计
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CreditCardKeeperStatBean extends JSFListingBean<CreditCardKeeperStatRequest, CreditCardKeeperStatResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    CreditCardKeeperStatService creditCardKeeperStatService;
    
    private StreamedContent xlsFile;
    private List<CommProductChannel> channelList;
    private List<CommUserType> userTypeList;
    
    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
    	InputStream input = new ByteArrayInputStream(creditCardKeeperStatService.getExcel($).getBytes("GBK"));
		String fileName = System.currentTimeMillis() + ".csv";
		xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
    	return xlsFile;
    }
    

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }
    
    private void initList(){
        userTypeList = creditCardKeeperStatService.getUserTypeList();
        channelList = creditCardKeeperStatService.getChannelList();
    }

    public void onFlushCache(){
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new CreditCardKeeperStatistisLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new CreditCardKeeperStatRequest();
        
        // 初始化默认当前日期
        Date date = null;
        try {
			date = sdf.parse(sdf.format(new DateTime().plusDays(-1).toDate()));
			$.setStartDate(date);
	        $.setEndDate(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        // 初始化默认选择全部
        List<Integer> defaultInt = new ArrayList<Integer>();
        defaultInt.add(-1000);
        $.setUserType(defaultInt);
        $.setChannelList(defaultInt);
        
        lazyDataModel = new CreditCardKeeperStatistisLazyDataModel($);
    }
    
}




